﻿using System;
using System.Collections.Generic;
using System.Windows.Automation;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using QuickUIT.Controls;

namespace QuickUIT.Tests
{
    [TestClass]
    public class UIListItemTests
    {
        private BaseState _baseState;

        [TestInitialize]
        public void TestSetup()
        {
            _baseState = new BaseState(TestUtilities.GetTestApplicationFileName(), "@Name='QuickUIT Test Application'");
            _baseState.Execute();
        }

        [TestMethod]
        public void ShouldGetIsSelectedProperty()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UIListItem listItem = window.UIList("@AutomationId='listBox1'").UIListItem("@Name='List Item 1'");
            Assert.IsFalse(listItem.IsSelected);
        }

        [TestMethod]
        public void ShouldSelectListItem()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UIListItem listItem = window.UIList("@AutomationId='listBox1'").UIListItem("@Name='List Item 8'");
            Assert.IsFalse(listItem.IsSelected);
            listItem.Select();
            Assert.IsTrue(listItem.IsSelected);
        }

        [TestMethod]
        public void ShouldScrollItemIntoView()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UIListItem listItem = window.UIList("@AutomationId='listBox1'").UIListItem("@Name='List Item 8'");
            Assert.IsTrue(listItem.IsOffscreen);
            listItem.ScrollIntoView();
            Assert.IsFalse(listItem.IsOffscreen);
        }

        [TestMethod]
        public void ShouldAddToSelection()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UIListItem listItem = window.UIList("@AutomationId='listBox1'").UIListItem("@Name='List Item 1'");
            Assert.IsFalse(listItem.IsSelected);
            listItem.AddToSelection();
            Assert.IsTrue(listItem.IsSelected);
        }

        [TestMethod]
        public void ShouldRemoveFromSelection()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UIListItem listItem = window.UIList("@AutomationId='listBox1'").UIListItem("@Name='List Item 1'");
            Assert.IsFalse(listItem.IsSelected);
            listItem.AddToSelection();
            Assert.IsTrue(listItem.IsSelected);
            listItem.RemoveFromSelection();
            Assert.IsFalse(listItem.IsSelected);
        }

        [TestMethod]
        public void ShouldGetSelectionContainer()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UIListItem listItem = window.UIList("@AutomationId='listBox1'").UIListItem("@Name='List Item 1'");
            AutomationElement selectionContainer = listItem.GetSelectionContainer();
            Assert.IsNotNull(selectionContainer);
            Assert.AreEqual("listBox1", selectionContainer.Current.AutomationId);
        }

        [TestMethod]
        public void ShouldGetExpandCollapseState()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UIListItem listItem = window.UIList("@AutomationId='listBox1'").UIListItem("@Name='List Item 1'");
            Assert.IsFalse(listItem.ExpandCollapseState.HasValue);
        }

        [TestMethod]
        public void ShouldGetToggleState()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UIListItem listItem = window.UIList("@AutomationId='listBox1'").UIListItem("@Name='List Item 1'");
            Assert.IsFalse(listItem.ToggleState.HasValue);
        }

        [TestMethod]
        public void ShouldGetRow()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UIListItem listItem = window.UIList("@AutomationId='listBox1'").UIListItem("@Name='List Item 1'");
            Assert.AreEqual(-1, listItem.Row);
        }

        [TestMethod]
        public void ShouldGetRowSpan()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UIListItem listItem = window.UIList("@AutomationId='listBox1'").UIListItem("@Name='List Item 1'");
            Assert.AreEqual(-1, listItem.RowSpan);
        }

        [TestMethod]
        public void ShouldGetColumn()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UIListItem listItem = window.UIList("@AutomationId='listBox1'").UIListItem("@Name='List Item 1'");
            Assert.AreEqual(-1, listItem.Column);
        }

        [TestMethod]
        public void ShouldGetValue()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UIListItem listItem = window.UIList("@AutomationId='listBox1'").UIListItem("@Name='List Item 1'");
            Assert.IsNull(listItem.Value);
        }

        [TestMethod]
        public void ShouldGetColumnSpan()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UIListItem listItem = window.UIList("@AutomationId='listBox1'").UIListItem("@Name='List Item 1'");
            Assert.AreEqual(-1, listItem.ColumnSpan);
        }

        [TestMethod]
        public void ShouldGetListItem()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UIListItem listItem1 = window.UIList("@AutomationId='listBox1'").UIListItem("[0]");
            Assert.AreEqual("List Item 1", listItem1.Name);

            listItem1 = window.UIList("@AutomationId='listBox1'").UIListItem("[@Name='List Item 1'][0]");
            Assert.AreEqual("List Item 1", listItem1.Name);
        }

        [TestMethod]
        public void ShouldGetAllListItems()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            List<UIListItem> listItems = window.UIList("@AutomationId='listBox1'").UIListItems();
            Assert.AreEqual(8, listItems.Count);
            for (int i = 0; i < listItems.Count; i++ )
            {
                Assert.AreEqual(String.Format("List Item {0}", i + 1), listItems[i].Name);   
            }
        }

        [TestCleanup]
        public void TestTeardown()
        {
            _baseState.Cleanup();
        }
    }
}